package com.webdesign.action;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.webdesign.dto.MyPage;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


@Controller("HomepageAction")
@Scope("prototype")
public class HomepageAction {
	
	private Map root = new HashMap();
	
	private String propath = ServletActionContext.getRequest().getRealPath("");
	
	private String path = ServletActionContext.getRequest().getContextPath();
	
	private Map<String,Object> session = ActionContext.getContext().getSession();
	
	private String username = (String) session.get("username");
	
	private Configuration cfg = new Configuration();
	
	
	public String setBasicInfo() throws IOException, TemplateException{
		
		cfg.setDirectoryForTemplateLoading(new File(propath + "/template"));
		root.put("path",path);
		root.put("username",username);
		Template template = cfg.getTemplate("/admin/basicinfosetting.ftl");
		Writer out = new OutputStreamWriter(System.out); 
		template.process(root, out);
		out.flush();
		
		return "success";
	}
	
	public String addDisplay() throws IOException, TemplateException{
		
		cfg.setDirectoryForTemplateLoading(new File(propath + "/template"));
		root.put("path",path);
		root.put("username",username);
		Template template = cfg.getTemplate("/admin/adddisplay.ftl");
		Writer out = new OutputStreamWriter(System.out); 
		template.process(root, out);
		out.flush();
		
		return "success";
	}
	
	public String showSubmit() throws IOException, TemplateException{
		
		cfg.setDirectoryForTemplateLoading(new File(propath + "/template"));
		root.put("path",path);
		root.put("username",username);
		Template template = cfg.getTemplate("/admin/submithomeworks.ftl");
		Writer out = new OutputStreamWriter(System.out); 
		template.process(root, out);
		out.flush();
		
		return "success";
	}
	
	public String showUnsubmit() throws IOException, TemplateException{
		
		cfg.setDirectoryForTemplateLoading(new File(propath + "/template"));
		root.put("path",path);
		root.put("username",username);
		Template template = cfg.getTemplate("/admin/unsubmithomeworks.ftl");
		Writer out = new OutputStreamWriter(System.out); 
		template.process(root, out);
		out.flush();
		
		return "success";
	}


	public Map getRoot() {
		return root;
	}


	public void setRoot(Map root) {
		this.root = root;
	}

	
}
